<?php

	/**
	 * json  0失败  1成功  2跳转  3弹窗
	 */

	namespace app\api\controller;


	use app\admin\model\scheduling\SchedulingModel;
	use core\basic\Controller;


	class SchedulingController extends Controller
	{
		//用户申请排单
		protected $model;

		public function __construct()
		{

			$this->model = new SchedulingModel();

		}
//获取当面会员排单的数据
		public function Scheduling()
		{
			$Memcache = $this->Memcache(2, session('uid')); //当前会员信息

			$UserSchedu = model('admin.scheduling.Schelist')->ScheInfo(['user_id'=>$Memcache->id]);
			json(1,$UserSchedu);
		}

//获取排单设置
		public function SchedulingInfo()
		{

			$UserSchedu = $this->model->GetUserSchedu();

			json(1, $UserSchedu);
		}


		//获取今天排单人数接口
		public function SchedulingList()
		{
			$today = strtotime(date('Y-m-d 00:00:00'));

			$where['add_time'] = array('egt', $today);
			$ProportionList = $this->model->getProportion();

			foreach ($ProportionList as $k => $v) {
				if (strtotime($v->add_time) >= $today) {
					$ret[$k] = $v;

				}
			}
			if (!!$ret) {
				$data = array(
					'data' => $ret,
					'count' => count($ret),
				);
			} else {
				$data['count'] = 0;
			}
			json(1, $data);
		}

//申请排单接口
		public function add_schedu()
		{
			$scheduling_id = request('scheduling_id', 'num', true, '排单ID');//排单ID
			$SingleAmount = request('SingleAmount', 'int', true, '排单金额');//排单金额
			$Memcache = $this->Memcache(2, session('uid')); //当前会员信息
			$userInfo = model('admin.member.Member')->getMember($Memcache->id);//当前会员信息

			if ($userInfo->status == 0) {
				json(0, '账号被锁定！');
			}
			if ($userInfo->gid == 1) {
				json(0, '账号未激活,无法排单！');
			}

			$schedulingInfo = $this->model->GetUserSchedu($scheduling_id);
			if ($schedulingInfo->Scheduling_group == 0) {
				if ($schedulingInfo->Scheduling_state != 2) {
					json(0, '排单未开始,或已经结束！');
				}
				$profit = request('profit', 'null', true, '排单天数');
				$profitInfo = $this->model->getProfit($profit);
				if ($SingleAmount % $profitInfo->Minimum_amount != 0) {
					json(0, '排单金额不正确！');
				}
				if ($userInfo->buyquota < $SingleAmount) {
					json(0, '排单额度不够！');
				}
				if ($schedulingInfo->Scheduling_current_amount < $SingleAmount) {
					json(0, '排单池不够！');
				}
				$NewScheduling_amount = $schedulingInfo->Scheduling_current_amount - $SingleAmount;//排单池剩余金额
				$newAmount = floatval($userInfo->buyquota) - floatval($SingleAmount);//排单之后用户的余额
				$profitStartTime = get_datetime();//排单开始时间
				$profitEndTime = get_datetime();//排单结束时间

				try {
//                $this->model->begin();
					//这里执行 自动添加程序

					$UserPLog = $userInfo->usermobile . '消耗' . $SingleAmount . '排单金额开始排单';
					if (!!$this->model->modScheduling(['Scheduling_current_amount' => $NewScheduling_amount]) and !!model('admin.member.Member')->modMember($userInfo->id, ['buyquota' => $newAmount]) and !!$this->model->addProportion(['user_id' => $userInfo->id, 'scheduling_id' => $profitInfo->id, 'quota' => $SingleAmount, 'odd' => create_code(8), 'add_time' => get_datetime(), 'status' => 1])) {
						model('admin.member.Bill')->AddBill($userInfo->id, 5, $UserPLog, 5);
						json(1, '排单成功！');
						$this->model->commit();
					} else {
						$this->model->rollback();
						json(0, error());
					}
				} catch (Exception $e) {
					$this->model->rollback();
					json(0, error());
				}
			}


		}

		//计算排单利润
		public function single_profit()
		{
			$profit = $this->model->profitList(['status' => 1]);
			$data = array();
			$i = 0;
			foreach ($profit as $k => $v) {
				$data[$i]['id'] = $v->id;
				$data[$i]['value'] = '排单' . $v->buy_days . '天' . '结算利润' . $v->Settlement_profits . '%';
				$i++;
			}
			json(1, $data);
		}

		//会员抢购订单池
		public function Snap_lines()
		{
			if (!!$SnapInfo = $this->model->PoolInfo(['status' => 1])) {
				json(1, $SnapInfo);
			}
		}

		//修改排单状态

		public function ModPool()
		{
			$pid = request('id', 'num', true, '排单池');//排单池
			$data['status'] = 2;
			if (!!$this->model->ModPool($pid, $data)) {
				json(1, '订单池结束');
			}
		}

		/**
		 * 会员抢购排单记录
		 */
		public function Buy_single()
		{
			$pid = request('pid', 'num', true, '排单池ID');//排单ID
			$quota = request('quota', 'num', true, '额度');//排单额度
			$Memcache = $this->Memcache(2, session('uid'));
			$userInfo = model('admin.member.Member')->getMember($Memcache->id);//当前会员信息
			if (!$PoolInfo = $this->model->PoolInfo(['id' => $pid])) {
				json(0, '请勿操作');
			}
			if ($PoolInfo->statu == 2 or strtotime($PoolInfo->end_time) <= time()) {
				json(0, '排单池已经关闭,请稍后尝试！');
			}
			if ($PoolInfo->sauota < $quota) {
				json(0, '排单池额度不足！');
			}
			if ($userInfo->up_integral < $quota) {
				json(6, '资金不足,请先充值!');
			}
			if ($quota < 10000) {
				json(6, '抢购金额最小10000');
			}
			$UuserNewAmount = floatval($userInfo->up_integral) - floatval($quota);//排单池购买之后用户的余额
			//信息添加列表
			$data = array(
				'user_id' => $userInfo->id,
				'pool_id' => $PoolInfo->id,
				'quota' => $quota,
				'statu' => 1,
				'add_time' => get_datetime(),
			);
			//购买后订单池额度
			$pooldata = array(
				'sauota' => floatval($PoolInfo->sauota) - floatval($quota)
			);
			$userData = array(
				'up_integral' => $UuserNewAmount,
				'buyquota' => $quota,
			);
//开启事务
			try {
				$userCLog = $userInfo->usermobile . '消耗' . $quota . '充值积分抢购排单额度';
				$UserPLog = $userInfo->usermobile . '获取' . $quota . '排单额度';
				if (!!$this->model->addUserPool($data) and !!model('admin.member.Member')->modMember($userInfo->id, $userData) and !!$this->model->ModPool($PoolInfo->id, $pooldata)) {
					model('admin.member.Bill')->AddBill($userInfo->id, 1, $userCLog, 1);
					model('admin.member.Bill')->AddBill($userInfo->id, 3, $UserPLog, 5);
					json(1, '排单成功！');
					$this->model->commit();
				} else {
					$this->model->rollback();
					json(0, error());
				}
			} catch (Exception $e) {
				$this->model->rollback();
				json(0, error());

			}
		}

	}